# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=avogadroapp
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.100.0
pkgrel=1
pkgdesc='Advanced molecular editor designed for cross-platform use in computational chemistry, molecular modeling, bioinformatics, materials science, and related areas (mingw-w64)'
arch=(any)
mingw_arch=(ucrt64 clang64 clangarm64)
url='https://www.openchemistry.org/'
msys2_repository_url="https://github.com/OpenChemistry/avogadroapp"
license=(spdx:BSD-3-Clause)
depends=("${MINGW_PACKAGE_PREFIX}-avogadrolibs"
         "${MINGW_PACKAGE_PREFIX}-avogadrolibs-qt6"
         "${MINGW_PACKAGE_PREFIX}-molequeue"
         "${MINGW_PACKAGE_PREFIX}-qt6-5compat"
         "${MINGW_PACKAGE_PREFIX}-qt6-base")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-eigen3"
             "${MINGW_PACKAGE_PREFIX}-vtk"
             "${MINGW_PACKAGE_PREFIX}-fast_float"
             "${MINGW_PACKAGE_PREFIX}-nlohmann-json"
             "${MINGW_PACKAGE_PREFIX}-utf8cpp")
source=(https://github.com/OpenChemistry/avogadroapp/archive/${pkgver}/${_realname}-${pkgver}.tar.gz
        https://github.com/OpenChemistry/avogadro-i18n/archive/${pkgver}/avogadro-i18n-${pkgver}.tar.gz
        001-fix-build-on-mingw.patch)
sha256sums=('8d73f5d24883ebe1a6ca54e13cf70aaf8125174190aa028e1e0ef48b95bd6b0b'
            '4081156389c11998e2f96f9294d2ae3d2af0115b1c92371cd057a66406e676bf'
            'dd4f67e8f80ca96f024a142831ef1ba5cd6c57dca6590230a44b9268e6302508')

prepare() {
  mv avogadro-i18n-${pkgver} avogadro-i18n

  cd ${_realname}-${pkgver}
  patch -p1 -i "${srcdir}"/001-fix-build-on-mingw.patch
}

build() {
  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
    "${_extra_config[@]}" \
    -DAvogadro_ENABLE_RPC=OFF \
    -DUSE_VTK=ON \
    -DQT_VERSION=6 \
    -B build-${MSYSTEM} \
    -S ${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build build-${MSYSTEM}
}

package() {
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install build-${MSYSTEM}

  install -Dm644 ${_realname}-${pkgver}/LICENSE -t "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}
}
